﻿using System;
using System.Reflection;
using System.Collections.Generic;

using LeanEngine.Extensions;
using LeanEngine.Collections;
using System.Text;

namespace LeanEngine.Validation
{
    /// <summary>
    /// Facade for validation services.
    /// </summary>
    public static class ValidationHelper
    {
        /// <summary>
        /// Validates <paramref name="value"/> using the specified validation attributes 
        /// and throw an exception in case of validation failure.
        /// </summary>
        /// <param name="value">The value to be validated.</param>
        /// <param name="validatorList">A validation rules list.</param>
        /// <exception cref="LeanEngine.Exception.BusinessException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static bool Validate(object value, ICollection<ValidationAttribute> validatorList)
        {
            if (validatorList.IsNull()) throw new ArgumentNullException("validatorList");

            foreach (var validator in validatorList)
            {
                validator.Validate(value);
            }

            return true;
        }

        /// <summary>
        /// Validates <paramref name="value"/> using the specified validation attributes 
        /// and add the error message to the container in case of validation failure.
        /// </summary>
        /// <param name="value">The value to be validated.</param>
        /// <param name="validatorList">A validation rules list.</param>
        /// <param name="errorMessageContainer">The container for all the error messages.</param>
        /// <exception cref="ArgumentException"></exception>
        public static void Validate(object value, ICollection<ValidationAttribute> validatorList, StringBuilder errorMessageContainer)
        {
            if (validatorList.IsNull()) throw new ArgumentNullException("validatorList");

            foreach (var validator in validatorList)
            {
                validator.Validate(value, errorMessageContainer);
            }
        }
    }
}
